package pl.kemot.impl.customer.add;

import pl.kemot.customer.add.IPresenterCustomerAdd;
import pl.kemot.customer.add.IViewCustomerAdd;
import pl.kemot.impl.Context;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class ViewCustomerAddImpl extends CustomComponent implements IViewCustomerAdd {

	private Form formCustomer;
	private TextField textFieldId;
	private TextField textFieldFirstName;
	private TextField textFieldLastName;
	private TextField textFieldAge;
	private Label labelError;
	private Button buttonOK;
	private Button buttonReset;
	private Button buttonCancel;
	private IPresenterCustomerAdd presenterCustomerAdd;
	private Panel panel;

	public ViewCustomerAddImpl(String caption, IPresenterCustomerAdd presenterCustomerAdd) {
		super();
		this.presenterCustomerAdd = presenterCustomerAdd;
		this.presenterCustomerAdd.setViewCustomerAdd(this);

		panel = new Panel(caption);
		formCustomer = new Form();

		textFieldId = new TextField("Id");
		textFieldId.setEnabled(false);
		formCustomer.addField("id", textFieldId);

		textFieldFirstName = new TextField("Imię");
		formCustomer.addField("firstName", textFieldFirstName);

		textFieldLastName = new TextField("Nazwisko");
		formCustomer.addField("lastName", textFieldLastName);

		textFieldAge = new TextField("Wiek");
		formCustomer.addField("age", textFieldAge);

		labelError = new Label();

		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonOK = new Button("Dodaj");
		buttonReset = new Button("Wyczyść");
		buttonCancel = new Button("Anuluj");
		buttonBar.addComponent(buttonOK);
		buttonBar.addComponent(buttonReset);
		buttonBar.addComponent(buttonCancel);

		configureEventListeners();

		VerticalLayout footerLayout = new VerticalLayout();
		footerLayout.addComponent(labelError);
		footerLayout.addComponent(buttonBar);

		formCustomer.setFooter(footerLayout);

		panel.addComponent(formCustomer);
		setCompositionRoot(panel);
	}

	@Override
	public Integer getAge() {
		Integer age = null;
		try {
			String text = textFieldAge.getValue().toString();
			age = Integer.parseInt(text);
		} catch (NumberFormatException e) {
		}
		return age;
	}

	@Override
	public String getFirstName() {
		String text = textFieldFirstName.getValue().toString();
		return text;
	}

	@Override
	public String getLastName() {
		String text = textFieldLastName.getValue().toString();
		return text;
	}

	@Override
	public void setError(ErrorFieldsFlags errorFieldsFlags, String message) {
		labelError.setValue(message);
	}

	@Override
	public Component getComponent() {
		return this;
	}

	private void configureEventListeners() {
		buttonOK.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				presenterCustomerAdd.perform(new Context());
			}
		});
		buttonReset.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				formCustomer.getField("firstName").setValue("");
				formCustomer.getField("lastName").setValue("");
				formCustomer.getField("age").setValue("");
			}
		});
		buttonCancel.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				Component component = getComponent();
				ComponentContainer componentContainer = (ComponentContainer) component.getParent();
				componentContainer.removeComponent(component);
			}
		});
	}
}
